package org.lanqiao.tjut.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.lanqiao.tjut.bean.AdminBean;
import org.lanqiao.tjut.bean.StudentBean;
import org.lanqiao.tjut.bean.TeacherBean;
import org.lanqiao.tjut.db.DBDriverFactory;

public class UserInfoManagerDao {

	

	/**
	 * 保存教师信息
	 * 
	 * @param tea
	 *            教师信息对象
	 * @return 保存结果记录行数
	 */
	public int savaTeaInfo(TeacherBean tea) {
		int re_i = 0;
		if (tea.getTeaid() == null) {
			// 新增处理
			String strSql = "INSERT INTO PDTEACHER(TEANAME,TEAPSW,TEASEX,TEAAGE,TEATEL,TEAADDRESS,TEAIDEFTITY)VALUES(?,?,?,?,?,?,?)";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { tea.getTeaname(), tea.getTeapsw(), tea.getTeasex(), tea.getTeaage(), tea.getTeatel(),
							tea.getTeaaddress(), tea.getTeaideftity() });
		} else {
			// 修改处理
			String strSql = "UPDATE PDTEACHER  SET TEANAME =?,TEAPSW=?,TEASEX=?,TEAAGE=?,TEATEL=?,TEAADDRESS=?,TEAIDEFTITY=? WHERE TEAID =? ";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { tea.getTeaname(), tea.getTeapsw(), tea.getTeasex(), tea.getTeaage(), tea.getTeatel(),
							tea.getTeaaddress(), tea.getTeaideftity(), tea.getTeaid() });

		}
		return re_i;
	}

	/**
	 * 保存学生信息
	 * 
	 * @param stu
	 *            学生信息对象
	 * @return 保存结果记录行数
	 */
	public int savaStuInfo(StudentBean stu) {
		int re_i = 0;
		if (stu.getStuid() == null) {
			// 新增处理
			String strSql = "INSERT INTO PDSTUDENT(STUNAME,STUPSW,STUSEX,STUAGE,STUTEL,STUADDRESS,STUIDEFTITY)VALUES(?,?,?,?,?,?,?)";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { stu.getStuname(), stu.getStupsw(), stu.getStusex(), stu.getStuage(), stu.getStutel(),
							stu.getStuaddress(), stu.getStuideftity() });
		} else {
			// 修改处理
			String strSql = "UPDATE PDSTUDENT  SET STUNAME =?,STUPSW=?,STUSEX=?,STUAGE=?,STUTEL=?,STUADDRESS=?,STUIDEFTITY=? WHERE STUID =? ";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { stu.getStuname(), stu.getStupsw(), stu.getStusex(), stu.getStuage(), stu.getStutel(),
							stu.getStuaddress(), stu.getStuideftity(), stu.getStuid() });

		}
		return re_i;
	}

	/**
	 * 保存管理员信息
	 * 
	 * @param admin
	 *            管理员信息对象
	 * @return 保存结果记录行数
	 */
	public int savaAdminInfo(AdminBean admin) {
		int re_i = 0;
		if (admin.getAdminid() == null) {
			// 新增处理
			String strSql = "INSERT INTO PDADMIN(ADMINNAME,ADMINPSW,ADMINSEX,ADMINAGE,ADMINTEL,ADMINADDRESS,ADMINIDEFTITY)VALUES(?,?,?,?,?,?,?)";
			re_i = DBDriverFactory.getDBDriverInstance()
					.update(strSql,
							new Object[] { admin.getAdminname(), admin.getAdminpsw(), admin.getAdminsex(),
									admin.getAdminage(), admin.getAdmintel(), admin.getAdminaddress(),
									admin.getAdminideftity() });
		} else {
			// 修改处理
			String strSql = "UPDATE PDADMIN  SET ADMINNAME =?,ADMINPSW=?,ADMINSEX=?,ADMINAGE=?,ADMINTEL=?,ADMINADDRESS=?,ADMINIDEFTITY=? WHERE ADMINID =? ";
			re_i = DBDriverFactory.getDBDriverInstance().update(strSql,
					new Object[] { admin.getAdminname(), admin.getAdminpsw(), admin.getAdminsex(), admin.getAdminage(),
							admin.getAdmintel(), admin.getAdminaddress(), admin.getAdminideftity(),
							admin.getAdminid() });

		}
		return re_i;
	}



	/**
	 * 查询所有教师数据信息
	 * 
	 * @return 所有教师数据信息集合
	 */
	public List<TeacherBean> getAllTeaInfo() {
		String strSql = "SELECT * FROM PDTEACHER ORDER BY TEAID DESC";
		List<TeacherBean> lstuser = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<TeacherBean>(TeacherBean.class));
		return lstuser;
	}

	/**
	 * 查询所有学生数据信息
	 * 
	 * @return 所有学生数据信息集合
	 */
	public List<StudentBean> getAllStuInfo() {
		String strSql = "SELECT * FROM PDSTUDENT ORDER BY STUID DESC";
		List<StudentBean> lstuser = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<StudentBean>(StudentBean.class));
		return lstuser;
	}

	/**
	 * 查询所有管理员数据信息
	 * 
	 * @return 所有管理员数据信息集合
	 */
	public List<AdminBean> getAllAdminInfo() {
		String strSql = "SELECT * FROM PDADMIN ORDER BY ADMINID DESC";
		List<AdminBean> lstuser = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<AdminBean>(AdminBean.class));
		return lstuser;
	}


	/**
	 * 根据教师名获取教师信息
	 * 
	 * @param tea
	 *            教师对象
	 * @return 该教师信息集合
	 */
	public List<TeacherBean> getQueryTeaInfoByName(TeacherBean tea) {
		String strSql = "SELECT * FROM PDTEACHER WHERE TEANAME = ?";
		List<TeacherBean> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<>(TeacherBean.class), new Object[] { tea.getTeaname() });
		return lst;
	}
	
	/**
	 * 根据学生名获取学生信息
	 * 
	 * @param tea
	 *            学生对象
	 * @return 该教师信息集合
	 */
	public List<StudentBean> getQueryStuInfoByName(StudentBean stu) {
		String strSql = "SELECT * FROM PDSTUDENT WHERE STUNAME = ?";
		List<StudentBean> lst = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanListHandler<>(StudentBean.class), new Object[] { stu.getStuname() });
		return lst;
	}

	/**
	 * 查询教师数据信息(模糊查询)
	 * 
	 * @param tea
	 *            教师信息对象
	 * @return 查询的教师信息
	 */
	public List<TeacherBean> getQueryTeaInfo(TeacherBean tea) {
		// 根据条件进行数据查询，没有输入查询条件，则进行全部查询

		StringBuilder strSql = new StringBuilder("SELECT * FROM PDTEACHER WHERE 1=1");

		// 根据查询条件动态拼写查询语句和参数
		List<Object> lstParams = new ArrayList<Object>();

		if (tea.getTeaname() != null && !tea.getTeaname().trim().equals("")) {
			// 姓名查询

			strSql.append(" AND TEANAME LIKE ?");
			lstParams.add("%%" + tea.getTeaname() + "%%");
		}
		if (tea.getTeasex() != null && !tea.getTeasex().trim().equals("")) {
			// 性别查询
			strSql.append(" AND TEASEX LIKE ?");
			lstParams.add("%%" + tea.getTeasex() + "%%");
		}
		if (tea.getTeaage() > 0) {
			// 年龄查询
			strSql.append(" AND TEAAGE >= ?");
			lstParams.add(tea.getTeaage());
		}
		if (tea.getTeaage2() > 0) {
			// 年龄查询
			strSql.append(" AND TEAAGE <= ?");
			lstParams.add(tea.getTeaage2());
		}
		if (tea.getTeatel() != null && !tea.getTeatel().trim().equals("")) {
			// 电话查询
			strSql.append(" AND TEATEL LIKE ?");

			lstParams.add("%%" + tea.getTeatel() + "%%");
		}
		if (tea.getTeaaddress() != null && !tea.getTeaaddress().trim().equals("")) {
			// 电话查询
			strSql.append(" AND TEAADDRESS LIKE ?");

			lstParams.add("%%" + tea.getTeaaddress() + "%%");
		}
		// 给查询语句添加排序
		strSql.append(" ORDER BY TEAID");

		List<TeacherBean> lsttea = DBDriverFactory.getDBDriverInstance().query(strSql.toString(),
				new BeanListHandler<TeacherBean>(TeacherBean.class), lstParams.toArray());

		return lsttea;
	}

	/**
	 * 查询学生数据信息(模糊查询)
	 * 
	 * @param tea
	 *            学生信息对象
	 * @return 查询的学生信息
	 */
	public List<StudentBean> getQueryStuInfo(StudentBean stu) {
		// 根据条件进行数据查询，没有输入查询条件，则进行全部查询

		StringBuilder strSql = new StringBuilder("SELECT * FROM PDSTUDENT WHERE 1=1");

		// 根据查询条件动态拼写查询语句和参数
		List<Object> lstParams = new ArrayList<Object>();

		if (stu.getStuname() != null && !stu.getStuname().trim().equals("")) {
			// 姓名查询

			strSql.append(" AND STUNAME LIKE ?");
			lstParams.add("%%" + stu.getStuname() + "%%");
		}
		if (stu.getStusex() != null && !stu.getStusex().trim().equals("")) {
			// 性别查询
			strSql.append(" AND STUSEX LIKE ?");
			lstParams.add("%%" + stu.getStusex() + "%%");
		}
		if (stu.getStuage() > 0) {
			// 年龄查询
			strSql.append(" AND STUAGE >= ?");
			lstParams.add(stu.getStuage());
		}
		if (stu.getStuage2() > 0) {
			// 年龄查询
			strSql.append(" AND STUAGE <= ?");
			lstParams.add(stu.getStuage2());
		}
		if (stu.getStutel() != null && !stu.getStutel().trim().equals("")) {
			// 电话查询
			strSql.append(" AND STUTEL LIKE ?");

			lstParams.add("%%" + stu.getStutel() + "%%");
		}
		if (stu.getStuaddress() != null && !stu.getStuaddress().trim().equals("")) {
			// 电话查询
			strSql.append(" AND STUADDRESS LIKE ?");

			lstParams.add("%%" + stu.getStuaddress() + "%%");
		}
		// 给查询语句添加排序
		strSql.append(" ORDER BY STUID");

		List<StudentBean> lststu = DBDriverFactory.getDBDriverInstance().query(strSql.toString(),
				new BeanListHandler<StudentBean>(StudentBean.class), lstParams.toArray());

		return lststu;
	}

	/**
	 * 查询管理员数据信息(模糊查询)
	 * 
	 * @param admin
	 *            管理员信息对象
	 * @return 查询的管理员信息
	 */
	public List<AdminBean> getQueryAdminInfo(AdminBean admin) {
		// 根据条件进行数据查询，没有输入查询条件，则进行全部查询

		StringBuilder strSql = new StringBuilder("SELECT * FROM PDADMIN WHERE 1=1");

		// 根据查询条件动态拼写查询语句和参数
		List<Object> lstParams = new ArrayList<Object>();

		if (admin.getAdminname() != null && !admin.getAdminname().trim().equals("")) {
			// 姓名查询

			strSql.append(" AND ADMINNAME LIKE ?");
			lstParams.add("%%" + admin.getAdminname() + "%%");
		}
		if (admin.getAdminsex() != null && !admin.getAdminsex().trim().equals("")) {
			// 性别查询
			strSql.append(" AND ADMINSEX LIKE ?");
			lstParams.add("%%" + admin.getAdminsex() + "%%");
		}
		if (admin.getAdminage() > 0) {
			// 年龄查询
			strSql.append(" AND ADMINAGE >= ?");
			lstParams.add(admin.getAdminage());
		}
		if (admin.getAdminage2() > 0) {
			// 年龄查询
			strSql.append(" AND ADMINAGE <= ?");
			lstParams.add(admin.getAdminage2());
		}
		if (admin.getAdmintel() != null && !admin.getAdmintel().trim().equals("")) {
			// 电话查询
			strSql.append(" AND ADMINTEL LIKE ?");

			lstParams.add("%%" + admin.getAdmintel() + "%%");
		}
		if (admin.getAdminaddress() != null && !admin.getAdminaddress().trim().equals("")) {
			// 电话查询
			strSql.append(" AND ADMINADDRESS LIKE ?");

			lstParams.add("%%" + admin.getAdminaddress() + "%%");
		}
		// 给查询语句添加排序
		strSql.append(" ORDER BY ADMINID");

		List<AdminBean> lststu = DBDriverFactory.getDBDriverInstance().query(strSql.toString(),
				new BeanListHandler<AdminBean>(AdminBean.class), lstParams.toArray());

		return lststu;
	}

	/**
	 * 根据用户id删除该用户信息
	 * 
	 * @param userId
	 *            用户id
	 * @return 修改记录的行数
	 */
	public int deleteUserInfo(int userId) {
		String strSql = "DELETE  FROM PDUUSERS WHERE USERID = ?";
		int re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}

	/**
	 * 根据教师id删除该用户信息
	 * 
	 * @param userId
	 *            教师id
	 * @return 修改记录的行数
	 */
	public int deleteTeaInfo(int userId) {
		String strSql = "DELETE  FROM PDTEACHER WHERE TEAID = ?";
		int re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}

	/**
	 * 根据学生id删除该用户信息
	 * 
	 * @param userId
	 *            学生id
	 * @return 修改记录的行数
	 */
	public int deleteStuInfo(int userId) {
		String strSql = "DELETE  FROM PDSTUDENT WHERE STUID = ?";
		int re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}

	/**
	 * 根据管理员id删除该用户信息
	 * 
	 * @param userId
	 *            管理员id
	 * @return 修改记录的行数
	 */
	public int deleteAdminInfo(int userId) {
		String strSql = "DELETE  FROM PDADMIN WHERE ADMINID = ?";
		int re_i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { userId });
		return re_i;
	}

	

	/**
	 * 通过教师id获取主键对象
	 * 
	 * @param strUid
	 *            教师id
	 * @return 对象
	 */
	public TeacherBean getTeaInfoById(String strUid) {
		String strSql = "SELECT * FROM PDTEACHER WHERE TEAID =?";
		TeacherBean tea = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanHandler<TeacherBean>(TeacherBean.class), new Object[] { strUid });
		return tea;
	}

	/**
	 * 通过学生id获取主键对象
	 * 
	 * @param strUid
	 *            学生id
	 * @return 对象
	 */
	public StudentBean getStuInfoById(String strUid) {
		String strSql = "SELECT * FROM PDSTUDENT WHERE STUID =?";
		StudentBean stu = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanHandler<StudentBean>(StudentBean.class), new Object[] { strUid });
		return stu;
	}

	/**
	 * 通过管理员id获取主键对象
	 * 
	 * @param strUid
	 *            管理员id
	 * @return 对象
	 */
	public AdminBean getAdminInfoById(String strUid) {
		String strSql = "SELECT * FROM PDADMIN WHERE ADMINID =?";
		AdminBean admin = DBDriverFactory.getDBDriverInstance().query(strSql,
				new BeanHandler<AdminBean>(AdminBean.class), new Object[] { strUid });
		return admin;
	}
}
